package com.zhang.third.day02;

import com.zhang.third.utils.ClickEventSource;
import com.zhang.third.utils.Event;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @title: map、flatmap
 * @author: zhang
 * @date: 2022/4/2 12:24
 */
public class Example2 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env
                .addSource(new ClickEventSource())
                .map(new MapFunction<Event, String>() {
                    @Override
                    public String map(Event value) throws Exception {
                        return value.key;
                    }
                })
                .print("匿名类实现");

        env
                .addSource(new ClickEventSource())
                .map(new MyMap())
                .print("外部累实现");

        env
                .addSource(new ClickEventSource())
                .map(r -> r.key)
                .print("匿名函数实现");

        env
                .addSource(new ClickEventSource())
                .flatMap(new FlatMapFunction<Event, String>() {
                    @Override
                    public void flatMap(Event value, Collector<String> out) throws Exception {
                        out.collect(value.key);
                    }
                })
                .print("flatMap实现");

        env.execute();
    }

    public static class MyMap implements MapFunction<Event, String> {
        @Override
        public String map(Event value) throws Exception {
            return value.key;
        }
    }
}
